package com.guigu.qingcheng.servicebusiness.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.qingcheng.pojo.bussiness.TbAd;
import com.guigu.qingcheng.servicebusiness.mapper.TbAdMapper;
import com.guigu.qingcheng.servicebusiness.service.TbAdService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.qingcheng.utils.CacheKey;
import com.guigu.qingcheng.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@Service
public class TbAdServiceImpl extends ServiceImpl<TbAdMapper, TbAd> implements TbAdService {

    @Resource
    private TbAdMapper tbAdMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public R findPage(Map<String, Object> searchMap, Integer page, Integer size) {
        Page<TbAd> tbActivityPage = new Page<>(page,size);
        QueryWrapper queryWrapper = createQueryWrapper(searchMap);
        tbAdMapper.selectPage(tbActivityPage,queryWrapper);
        long total = tbActivityPage.getTotal();
        List<TbAd> records = tbActivityPage.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    @Override
    public TbAd findById(Integer id) {
      return tbAdMapper.selectById(id);
    }

    @Override
    public void insert(TbAd tbAd) {
        tbAdMapper.insert(tbAd);
        saveAdToRedisByPosition(tbAd.getPosition());//重新加载缓存
    }

    @Override
    public void update(TbAd tbAd) {
        //获取之前的广告位置
        String position = tbAdMapper.selectById(tbAd.getId()).getPosition();
        tbAdMapper.updateById(tbAd);
        saveAdToRedisByPosition(position);//更新
        if(!position.equals(tbAd.getPosition())){ //如果广告位置发生变化
            saveAdToRedisByPosition(tbAd.getPosition());
        }

    }

    @Override
    public void delete(Integer id) {
        String position = tbAdMapper.selectById(id).getPosition();
        saveAdToRedisByPosition(position);//重新加载缓存
        tbAdMapper.deleteById(id);
    }

    @Override
    public List<TbAd> findByPosition(String position) {
        //从缓存中查询广告列表
        System.out.println("从缓存中查询广告");
        return(List<TbAd>)redisTemplate.boundHashOps(CacheKey.AD.toString()).get(position);
    }

    /**
     * 返回所有的广告位置
     * @return
     */
    private List<String> getPositionList(){
        List<String> positionList=new ArrayList<String>();
        positionList.add("web_index_lb");//首页广告轮播图
        return positionList;
    }


    @Override
    public void saveAdToRedisByPosition(String position) {
        //查询某位置的广告列表
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("position",position);
        wrapper.le("start_time",new Date());
        wrapper.ge("end_time",new Date());
        wrapper.eq("status",1);
        List<TbAd> adList = tbAdMapper.selectList(wrapper);
        //装入缓存
        redisTemplate.boundHashOps(CacheKey.AD.toString()).put(position,adList);
    }

    @Override
    public void saveAllAdToRedis() {
        //循环所有的广告位置
        for(String position:getPositionList()){
            saveAdToRedisByPosition(position);
        }

    }

    // 根据查询条件封装模块对象
    private QueryWrapper createQueryWrapper(Map<String, Object> searchMap) {
        QueryWrapper<TbAd> wrapper = new QueryWrapper<>();
        if (searchMap != null) {
            if (searchMap.get("id") != null && !"".equals(searchMap.get("id")))
                wrapper.eq("id", searchMap.get("id"));
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name")))
                wrapper.like("name", "%" + searchMap.get("name") + "%");
            if (searchMap.get("position") != null && !"".equals(searchMap.get("position")))
                wrapper.eq("position",searchMap.get("position"));
            if (searchMap.get("startTime") != null && !"".equals(searchMap.get("startTime")))
                wrapper.ge("startTime",searchMap.get("startTime"));
            if (searchMap.get("endTime") != null && !"".equals(searchMap.get("endTime")))
                wrapper.le("endTime",searchMap.get("endTime"));
            if (searchMap.get("status") != null && !"".equals(searchMap.get("status")))
                wrapper.eq("status",searchMap.get("status"));
            if (searchMap.get("image") != null && !"".equals(searchMap.get("image")))
                wrapper.eq("image",searchMap.get("image"));
            if (searchMap.get("url") != null && !"".equals(searchMap.get("url")))
                wrapper.eq("url",searchMap.get("url"));
            if (searchMap.get("remarks") != null && !"".equals(searchMap.get("remarks")))
                wrapper.eq("remarks",searchMap.get("remarks"));
        }
        return  wrapper;
    }
}
